<template>
  <div class="login">
    <video src="@/assets/video/login-background.mp4" autoplay loop muted class="background-video"></video>

    <!-- 标题 -->
    <div class="title-dom">
      <div class="title-name">欢迎登录北斗位置服务平台</div>
      <div class="sub-title">Welcome</div>
    </div>

    <!-- 登录主体 -->
    <div class="login-dom">
      <el-form ref="loginRef" :model="loginForm" :rules="loginRules" class="login-form">
        <div class="form-title">
          欢迎登录
          <div class="split-line"></div>
          <span class="title-content">{{ systemTile }}</span>
        </div>
        <el-form-item prop="username">
          <el-input v-model="loginForm.username" type="text" size="large" auto-complete="off" placeholder="账号">
            <template #prefix>
              <svg-icon icon-class="user" class="el-input__icon input-icon" />
            </template>
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input v-model="loginForm.password" type="password" show-password size="large" auto-complete="off"
            placeholder="密码" @keyup.enter="handleLogin">
            <template #prefix><svg-icon icon-class="password" class="el-input__icon input-icon" /></template>
          </el-input>
        </el-form-item>
        <el-form-item prop="code" v-if="false">
          <el-input v-model="loginForm.code" size="large" auto-complete="off" placeholder="验证码" style="width: 63%"
            @keyup.enter="handleLogin">
            <template #prefix><svg-icon icon-class="validCode" class="el-input__icon input-icon" /></template>
          </el-input>
          <div class="login-code">
            <img :src="codeUrl" @click="getCode" class="login-code-img" />
          </div>
        </el-form-item>
        <el-checkbox v-model="loginForm.rememberMe" style="margin:0px 0px 25px 0px;">记住密码</el-checkbox>
        <el-form-item style="width:100%;">
          <div :loading="loading" class="login-btn" @click.prevent="handleLogin">
            <span v-if="!loading">登 录</span>
            <span v-else>登 录 中...</span>
          </div>
          <div class="scan-btn" @click.prevent="dialogVisible = true">
            <img src="@/assets/img/login/scan.png" />
            扫一扫，下载时空定位APP
          </div>
          <div style="float: right;" v-if="register">
            <router-link class="link-type" :to="'/register'">立即注册</router-link>
          </div>
        </el-form-item>
      </el-form>

      <div class="el-login-footer">
        <div>Copyright © 2023-2025 All Rights Reserved </div>
        <a href="https://beian.miit.gov.cn/">鄂ICP备2023012096号-1</a>
      </div>
    </div>

    <el-dialog v-model="dialogVisible" title="" width="400" :title="1" class="login-QRcode">
      <div class="close-dom">
        <el-icon @click="dialogVisible = false" style="cursor: pointer;">
          <CloseBold />
        </el-icon>
      </div>
      <div class="code-text">扫一扫，下载时空定位APP</div>
      <div class="code-img">
        <div class="img-item">
          <img src="@/assets/img/login/android.png" alt="安卓">
          <div><img src="@/assets/img/login/androidIcon.png" alt="安卓"> 安卓</div>
        </div>
      </div>
    </el-dialog>
  </div>
</template>

<script setup>
import { getCodeImg } from "@/api/login";
import Cookies from "js-cookie";
import { encrypt, decrypt } from "@/utils/jsencrypt";
import useUserStore from '@/store/modules/user'

const userStore = useUserStore()
const router = useRouter();
const { proxy } = getCurrentInstance();
const systemTile = import.meta.env.VITE_APP_TITLE
const loginForm = ref({
  username: "",
  password: "",
  rememberMe: false,
  code: "",
  uuid: ""
});
const dialogVisible = ref(false)
const loginRules = {
  username: [{ required: true, trigger: "blur", message: "请输入您的账号" }],
  password: [{ required: true, trigger: "blur", message: "请输入您的密码" }],
  code: [{ required: true, trigger: "change", message: "请输入验证码" }]
};

const codeUrl = ref("");
const loading = ref(false);
// 验证码开关
const captchaEnabled = ref(true);
// 注册开关
const register = ref(false);
const redirect = ref(undefined);

function handleLogin() {
  proxy.$refs.loginRef.validate(valid => {
    if (valid) {
      loading.value = true;
      // 勾选了需要记住密码设置在 cookie 中设置记住用户姓名和密码
      if (loginForm.value.rememberMe) {
        Cookies.set("username", loginForm.value.username, { expires: 30 });
        Cookies.set("password", encrypt(loginForm.value.password), { expires: 30 });
        Cookies.set("rememberMe", loginForm.value.rememberMe, { expires: 30 });
      } else {
        // 否则移除
        Cookies.remove("username");
        Cookies.remove("password");
        Cookies.remove("rememberMe");
      }
      // 调用action的登录方法
      userStore.login(loginForm.value).then(() => {
        router.push({ path: redirect.value || "/" });
      }).catch(() => {
        loading.value = false;
        // 重新获取验证码
        if (captchaEnabled.value) {
          getCode();
        }
      });
    }
  });
}

function getCode() {
  getCodeImg().then(res => {
    captchaEnabled.value = res.captchaEnabled === undefined ? true : res.captchaEnabled;
    if (captchaEnabled.value) {
      codeUrl.value = "data:image/gif;base64," + res.img;
      loginForm.value.uuid = res.uuid;
    }
  });
}

function getCookie() {
  const username = Cookies.get("username");
  const password = Cookies.get("password");
  const rememberMe = Cookies.get("rememberMe");
  loginForm.value = {
    username: username === undefined ? loginForm.value.username : username,
    password: password === undefined ? loginForm.value.password : decrypt(password),
    rememberMe: rememberMe === undefined ? false : Boolean(rememberMe)
  };
}

getCode();
getCookie();
</script>

<style lang='scss' scoped>
.login {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;

  .background-video {
    position: absolute;
    width: 100%;
    height: 100%;
    object-fit: cover;
    z-index: 0;
  }

  .login-dom {
    width: 430px;
    height: 500px;
    background: #FFFFFF;
    border-radius: 10px;
    position: absolute;
    z-index: 99;
    right: 15%;

    .form-title {
      margin: 0px auto;
      text-align: center;
      color: #333;
      font-size: 22px;
      font-weight: 600;
      display: flex;
      align-items: center;
      height: 30px;
      margin-bottom: 40px;

      .title-content {
        color: #286FFF;
        font-size: 16px;
        font-weight: 500;
      }

      .split-line {
        width: 1px;
        height: 18px;
        background-color: #E6E6E6;
        margin: 0 10px;
      }
    }

    .login-form {
      border-radius: 6px;
      background: #ffffff;
      padding: 45px 30px 20px 30px;


      .el-input {
        height: 40px;

        input {
          height: 40px;
        }
      }

      .input-icon {
        height: 39px;
        width: 14px;
        margin-left: 0px;
      }

      .login-btn {
        background: #286FFF;
        border-radius: 6px;
        width: 100%;
        height: 46px;
        font-size: 18px;
        display: flex;
        align-items: center;
        justify-content: center;
        cursor: pointer;
      }

      .scan-btn {
        border: 1px solid #286FFF;
        border-radius: 6px;
        width: 100%;
        height: 46px;
        font-size: 18px;
        display: flex;
        align-items: center;
        justify-content: center;
        margin-top: 16px;
        font-size: 16px;
        color: #286FFF;
        cursor: pointer;

        img {
          margin-right: 16px;
        }
      }
    }
  }

  .title-dom {
    margin-top: auto;
    margin-bottom: 50px;
    text-align: left;
    position: absolute;
    text-align: center;
    top: 9%;
    left: 22%;

    .title-name {
      font-weight: 600;
      font-size: 48px;
      width: max-content;
    }

    .sub-title {
      font-size: 30px;
    }
  }

  @media screen and (max-width: 1599px) {
    .title-dom {
      display: none;
    }
  }
}

.login-code {
  width: 33%;
  height: 40px;
  float: right;

  img {
    cursor: pointer;
    vertical-align: middle;
  }
}

.el-login-footer {
  bottom: 0;
  width: 100%;
  text-align: center;
  color: #999;
  font-family: Arial;
  font-size: 14px;
  letter-spacing: 1px;
}

.login-code-img {
  height: 40px;
  padding-left: 12px;
}

:deep(.code-text) {
  font-weight: 500;
  font-size: 16px;
  line-height: 20px;
  color: #333;
  text-align: center;
  margin-bottom: 30px;
}

:deep(.close-dom) {
  text-align: right;
  font-size: 20px;
  margin-bottom: 20px;
}

:deep(.code-img) {
  display: flex;
  justify-content: center;
  margin-bottom: 30px;

  .img-item {
    display: flex;
    align-items: center;
    flex-direction: column;

    >img {
      height: 258px;
      width: 258px;
    }

    >div {
      display: flex;
      align-items: center;
      justify-content: center;
      width: 84px;
      height: 34px;
      background: #F2F2F2;
      border-radius: 17px;
      font-size: 16px;
      margin-top: 20px;

      >img {
        margin-right: 5px;
      }
    }
  }
}
</style>
